//
// File.cpp for 206observ in /home/morel_t//Rendu/maths/206observ
// 
// Made by thomas morel
// Login   <morel_t@epitech.net>
// 
// Started on  Mon May  7 09:34:43 2012 thomas morel
// Last update Mon May  7 11:17:05 2012 thomas morel
//

#include	<iostream>
#include	<sstream>
#include	<fstream>
#include	"File.hh"

File::File(const std::string &filename) : filename_(filename)
{
  getLines(getFile());
}

File::~File()
{
}

std::string	File::getFilename() const
{
  return (this->filename_);
}

std::string	File::getLineByIndex(unsigned int index) const
{
  if (index < this->lines_.size())
    return (this->lines_[index]);
  return ("");
}

void		File::printFile() const
{
  std::vector<std::string>::const_iterator	it;

  for (it = this->lines_.begin(); it != this->lines_.end() && *it != ""; ++it)
    std::cout << *it << std::endl;
}

std::vector<std::string>	File::getLines() const
{
  return (this->lines_);
}

std::string		File::getFile() const
{
  std::ifstream		infile(this->filename_.c_str());
  std::stringstream	stream;

  if (infile.is_open())
    {
      while (infile.good())
	stream << static_cast<char>(infile.get());
      infile.close();
      return (stream.str());
    }
  return ("");
}

void		File::getLines(const std::string &file)
{
  int		size = file.size();
  int		pos = 0;

  for (int i = 0; i < size; ++i)
    {
      if (file[i] == '\n' || file[i] == '\0')
	{
	  this->lines_.push_back(file.substr(pos, i - pos));
	  pos = i + 1;
	}
    }
}
